package com.fc.advbox.controller.api;

import com.fc.advbox.common.ResponseJson;
import com.fc.advbox.controller.admin.BaseController;
import com.fc.advbox.entity.EquipmentRentEntity;
import com.fc.advbox.service.IEquipmentRentService;
import com.fc.advbox.utils.Constant;
import com.fc.advbox.utils.RRException;
import com.sun.xml.internal.rngom.parse.host.Base;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by E7450 on 2018/6/21.
 */
@RestController
@RequestMapping("/api/equipment/rent")
@Api(value="设备租赁接口", tags={"设备租赁接口"})
public class EquipmentRentController extends BaseController {

    @Autowired
    private IEquipmentRentService equipmentRentService;

    @ApiOperation(value = "POST -- 查询设备租聘记录",
            notes = " REST 方式请求" +
                    "Request Body: <ul>" +
                    "<li><i>可选参数: equipmentId: 设备Id</i></li>" +
                    "<li><i>可选参数: code: 设备编码 模糊查询</i></li>" +
                    "<li><i>可选参数: name: 设备名称，模糊查询</i></li>" +
                    "<li><i>可选参数: type: 设备类型，例如： WF-01, WF-02, WF-03, WF-04 </i></li>" +
                    "<li><i>可选参数: city: 地区, 例如：'Suzhou'</i></li>" +
                    "<li><i>可选参数: tenantId: 承租人ID</i></li>" +
                    "<li><i>默认参数：page: 1, 当前页，默认值1 </i></li>" +
                    "<li><i>默认参数：limit: 每次查询个数，默认为10</i></li>" +
                    "</ul>")
    @PostMapping("/list")
    @RequiresPermissions("api:equipment:view")
    public ResponseJson doQueryList(@RequestBody Map<String, Object> params) {
        //校对并赋值 offset和limit
        calculatePagination(params);

        List<EquipmentRentEntity> maintainList = equipmentRentService.queryList(params);
        return ResponseJson.success(maintainList);
    }


    @ApiOperation(value = "GET -- 查看单个设备维修信息",
            notes = " REST 方式请求" +
                    "/one/{rentId}",
            response = ResponseJson.class,
            httpMethod = "GET",
            consumes = "application/json",
            produces = "application/json",
            code = 200
    )
    @GetMapping("/one/{rentId}")
    @RequiresPermissions("api:equipment:view")
    public ResponseJson doQueryInfo(@PathVariable("rentId") Long rentId) {

        if(rentId == null || rentId == 0)
        {
            throw new RRException("ID不能为空!");
        }

        EquipmentRentEntity rentEntity = equipmentRentService.queryObject(rentId);
        return ResponseJson.success(rentEntity);
    }

    @ApiOperation(value = "POST -- 保存设备租聘信息",
            notes = " REST 方式请求" +
                    "Request Body: <ul>" +
                    "<li><i>必填：equipmentId: 设备ID</i></li>" +
                    "<li><i>必填：tenantId: 设备ID</i></li>" +
                    "<li><i>必填：startDate: 合约开始时间 ,例如：2018-06-27 08:10:37.80</i></li>" +
                    "<li><i>选填：endDate: 合约结束时间,例如：2018-06-27 08:10:37.80</i></li>" +
                    "</ul>")
    @PostMapping("/save")
    @RequiresPermissions("api:equipment:edit")
    public ResponseJson doSave(@RequestBody EquipmentRentEntity equipmentRent) {
        if(equipmentRent.getEquipmentId() == null || equipmentRent.getEquipmentId() == 0)
        {
            throw new RRException("equipmentId不能为空!");
        }
        if(equipmentRent.getTenantId() == null || equipmentRent.getTenantId() == 0)
        {
            throw new RRException("tenantId不能为空!");
        }
        if(equipmentRent.getStartDate() == null)
        {
            throw new RRException("startDate不能为空!");
        }

        equipmentRentService.save(equipmentRent);
        return ResponseJson.success();
    }

    @ApiOperation(value = "Put -- 更新设备租聘信息",
            notes = " REST 方式请求" +
                    "Request Body: <ul>" +
                    "<li><i>必填：id: 设备租聘ID</i></li>" +
                    "<li><i>必填：equipmentId: 设备ID</i></li>" +
                    "<li><i>必填：tenantId: 设备ID</i></li>" +
                    "<li><i>选填：startDate: 合约开始时间,例如：2018-06-27 08:10:37.80</i></li>" +
                    "<li><i>选填：endDate: 合约开始时间,例如：2018-06-27 08:10:37.80</i></li>" +
                    "</ul>")
    @PutMapping("/update")
    @RequiresPermissions("api:equipment:edit")
    public ResponseJson doUpdate(@RequestBody EquipmentRentEntity equipmentRent) {
        if(equipmentRent.getId() == null || equipmentRent.getId() == 0)
        {
            throw new RRException("rent主键不能为空!");
        }
        if(equipmentRent.getEquipmentId() == null || equipmentRent.getEquipmentId() == 0)
        {
            throw new RRException("equipmentId不能为空!");
        }
        if(equipmentRent.getTenantId() == null || equipmentRent.getTenantId() == 0)
        {
            throw new RRException("tenantId不能为空!");
        }

        equipmentRentService.update(equipmentRent);
        return ResponseJson.success();
    }

    @ApiOperation(value = "POST -- 批量删除设备租聘记录",
            notes = " REST 方式请求" +
                    "Request Body: <ul>")
    @PostMapping("/delete")
    @RequiresPermissions("api:equipment:edit")
    public ResponseJson doDetele(@RequestBody Long[] rentIds) {
        equipmentRentService.deleteBatch(rentIds);
        return ResponseJson.success();
    }
}
